package com.iwords.service.lucene;

import com.iwords.constants.Constants;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: qianxiang
 * Date: 11-5-1
 * Time: 下午7:04
 * To change this template use File | Settings | File Templates.
 */
public class IndexerFactory {
    private static IndexerFactory indexerFactory = new IndexerFactory();
    private IndexReader indexReaderSentence;
    private IndexSearcher indexSearcherSentence;
    private IndexReader indexReaderItem;
    private IndexSearcher indexSearcherItem;
    private FSDirectory sentenceDir = null;
    private FSDirectory itemDir = null;

    private IndexerFactory(){
        init();
    }

    private void init() {
       try {

           sentenceDir = FSDirectory.open(new File(Constants.SENTENCE_INDEX));
           itemDir = FSDirectory.open(new File(Constants.ITEM_INDEX));
//    	   sentenceDir = FSDirectory.open(new File("D:\\dev\\javaWorkSpace\\study\\iwordsSentenceIndex"));
//           itemDir = FSDirectory.open(new File("D:\\dev\\javaWorkSpace\\study\\iwordsItemIndex"));
           
		   indexReaderSentence = IndexReader.open(sentenceDir);
           indexSearcherSentence = new IndexSearcher(indexReaderSentence);
           indexReaderItem = IndexReader.open(itemDir);
           indexSearcherItem = new IndexSearcher(indexReaderItem);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public static IndexerFactory getFactory(){
		return indexerFactory;
	}

    public IndexSearcher getIndexSearcherSentence(){
        return indexSearcherSentence;
    }

    public IndexReader getIndexReaderSentence(){
        return indexReaderSentence;
    }

    public IndexReader getIndexReaderItem() {
        return indexReaderItem;
    }

    public IndexSearcher getIndexSearcherItem() {
        return indexSearcherItem;
    }
}
